package concurrent.aqs;

import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantLock;

public class InterruptExample {
    private static final ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {
        // 主线程先获取锁，模拟锁被占用
        lock.lock();

        Thread thread = new Thread(() -> {
            try {
                // 可中断地获取锁
                lock.lockInterruptibly();
                try {
                    System.out.println("线程获取到锁");
                } finally {
                    lock.unlock();
                }
            } catch (InterruptedException e) {
                System.out.println("线程在等待锁的过程中被中断，放弃获取锁");
            }
        });

        thread.start();

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 中断线程
        thread.interrupt();

        // 主线程释放锁
        lock.unlock();
    }
}